/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestInt.controlador;

import gestInt.DAO.PersonaJpaController;
import gestInt.DAO.UsuarioJpaController;
import gestInt.DAO.exceptions.NonexistentEntityException;
import gestInt.DAO.exceptions.RollbackFailureException;
import gestInt.modelo.Persona;
import gestInt.modelo.Usuario;
import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;
import org.primefaces.context.RequestContext;

/**
 *
 * @author elkin
 */
@Named(value = "personaController")
@SessionScoped
public class PersonaController implements Serializable {

    @Resource
    private UserTransaction utx;

    private Persona objPersona;
    private final PersonaJpaController jpaPersona;
    private List<Persona> lstPersonas;
    boolean encontrado;

    /**
     * Creates a new instance of PersonaController
     */
    public PersonaController() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("GestIntPU");
        this.objPersona = new Persona();
        this.jpaPersona = new PersonaJpaController(utx, emf);
        this.encontrado = false;
        this.lstPersonas = this.jpaPersona.findPersonaEntities();
    }

    public void registrarPersona() {
        this.lstPersonas = jpaPersona.findPersonaEntities();
        for (Persona persona : lstPersonas) {
            int codigo1 = this.objPersona.getIdpersona();
            int codigo2 = persona.getIdpersona();
            if (codigo1 == codigo2) {
                this.encontrado = true;
            }
        }

        if (!this.encontrado) {
            try {
                this.jpaPersona.create(objPersona);
            } catch (Exception ex) {
                Logger.getLogger(PersonaController.class.getName()).log(Level.SEVERE, null, ex);
            }
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Registro exitoso", null));
        } else {
            this.encontrado = false;
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "El codigo suministrado ya ha sido registrado", null));
        }

    }

    public void eliminarPersona() {

        try {
            int idEliminar = this.objPersona.getIdpersona();
            this.jpaPersona.destroy(idEliminar);
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("gestionPersonas.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(PersonaController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(PersonaController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(PersonaController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(PersonaController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void editarPersona() {

        try {
            this.jpaPersona.edit(this.objPersona);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(PersonaController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(PersonaController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(PersonaController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void limpiar() {
        System.out.println("en limpiar");
        RequestContext.getCurrentInstance().reset("form:panel");
    }

    public void limpiarObjpersona() {

    }
    
    public List<Persona> getLstPersonas() {
        return lstPersonas;
    }

    public void setLstPersonas(List<Persona> lstPersonas) {
        this.lstPersonas = lstPersonas;
    }

    public Persona getObjPersona() {
        return objPersona;
    }

    public void setObjPersona(Persona objPersona) {
        this.objPersona = objPersona;
    }

    public boolean isEncontrado() {
        return encontrado;
    }

    public void setEncontrado(boolean encontrado) {
        this.encontrado = encontrado;
    }

}
